<?php

namespace App\Http\Resources\Plane\PlaneOrder;

use App\Trip\Enums\TripTypes;
use App\Trip\IdentityCode\IdentityCode;
use App\Trip\Orders\TripOrderStateDesc;
use App\Trip\Passengers\OrderSubPassenger;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class PlaneOrderPassengerResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request)
    {
        $passenger = parent::toArray($request);
        if ($request->offsetGet('business') == 'plane_order_list') {
            return [
                'truename'       => $passenger['truename'],
                'passenger_type' => $passenger['passenger_type'],
            ];
        }
        $is_change_passenger  = $request->offsetGet('change_passenger') == 1;
        $request_passenger_id = $request->input('passenger_id');

        $passenger['order_state_desc'] = TripOrderStateDesc::transPassengerDescByOrderState(TripTypes::TRAIN, $passenger['order_state']);
        $passenger['passenger_id']     = OrderSubPassenger::buildSubPassengerId(
            $passenger['passenger_id'],
            $passenger['sequence']
        );

        // 遮挡身份证
        if (isset($passenger['identity_code'])) {
            $passenger['identity_code'] = (new IdentityCode($passenger['identity_code']))->hide();
        }

        if ($is_change_passenger) {
            $passenger['selected'] = $request_passenger_id == $passenger['passenger_id'];
        }
        unset(
            $passenger['created_at'],
            $passenger['updated_at'],
            $passenger['id'],
            $passenger['user_id'],
            $passenger['order_sn']
        );

        return collect($passenger)->except([
            'created_at',
            'updated_at',
            'id',
            'user_id',
            'order_sn',
        ])->toArray();
    }
}
